<?php
namespace Home\Controller;
use Think\Controller;
class LoginController extends Controller {
	/**
	 * 登录
	 */
    public function index()
    {
        $this->display();
    }
    public function login()
    {
        if (!IS_POST) {
            echo "1";die;
        }
        $data['username'] = $_POST['username'];
        $data['password'] = md5($_POST['password']);
        $user = M('user');
        $user_name = $user->where(array('name'=>$data['username']))->find();
        if (!isset($user_name)) {
            echo "2";die;    
        }
        if ($user_name['status'] == 1) {
            echo "3";die;
        }
        if (!$user->where(array('password'=>$data['password']))->find()) {
           echo "4";die;
        }
        $list = array(
            "id"        => $user_name['id'],
            "update_at" => date('Y-m-d H:i:s',time()),
            "user_ip"   => $_SERVER['SERVER_ADDR'],
            );
       if ($user->save($list)) {
            session('user_id',$user_name['id']);
            $url = U('Index/index');
            echo "5#".$url;
       }
    }
    /**
     * 退出登录
     */
    public function login_out()
    {
        session('user_id',null);
        redirect(U('Index/index'));
    }
    /**
     * 注册
     */
    public function register()
    {
        $this->display();
    }
    public function add()
    {
    	// var_dump($_POST);die;
    	if (!IS_POST) {
    		echo "1";die;
    	}
    	$data['name']        = $_POST['user_name'];
    	$data['username']    = $_POST['nick_name'];
    	$data['password']    = md5($_POST['pass_word']);
    	$data['rePass_word'] = md5($_POST['rePass_word']);
    	$data['created_at']  = date("Y-m-d H:i:s",time());
    	$data['user_ip'] 	 = $_SERVER['SERVER_ADDR'];
    	if ($data['password'] !== $data['rePass_word']) {
    		echo "2";die;
    	}
    	$user = M('user');
    	$userlist = $user->where(array('name'=>$data['name']))->find();
    	// print_r($data);die;
    	if (isset($userlist)) {
  	  		echo "3";die;
    	}

    	if ($user->add($data)) {
    		$url = U('Login/index');
    		echo "4#".$url;die;
    	}    	
    }
    
}